exports.__esModule = true;

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            * 事件立案
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            */


exports.default = class extends think.BaseSocket {
  /**
     * 事件立案申请，添加案件
     */

  applyCreateCaseAction() {
    var _this = this;

    return _asyncToGenerator(function* () {
      const aj = think.ajaxJson();
      const data = _this.post();
      const key = data.process_id;
      const id = data.org_id;
      const userid = data.userid;
      const overtime = data.overtime;
      const applyCaseService = _this.service('oa/CaseService');
      const apiBpmnService = _this.service('api/bpmn/BpmnService');
      const BpmnService = _this.service('bpmn/BpmnService');
      const res = yield BpmnService.startProcessInstance(key, id, userid, null, overtime);
      if (!res.error) {
        if (data.area_code && data.group_code) {
          data.ObjCode = data.area_code + data.group_code;
        }
        data.proc_inst_id = res.procInstId;
        const result = yield applyCaseService.createCase(data);
        if (result) {
          // let toUser = this.getOnlineModuleByUser('jdsl');
          // let obj = this.getSocketMsgBody(res);
          // this.sendMsgByUserAction(obj,toUser,'acceptScreen');
          const userIds = res.backLogsUserIds;
          // let socketBody = {
          //     result: {
          //         title: res.review_name + ',待您审批',
          //         id: res.procInstId,
          //         date: res.date
          //     },
          //     moduleName: 'xtbg',
          //     userIds: userid,
          //     menuUrl: 'myTask'
          // };
          const socketBody = _this.getSocketMsgBody();
          socketBody.result = {
            id: res.procInstId,
            date: res.date
          };
          socketBody.sendDate = (0, _moment2.default)().format('YYYY-MM-DD HH:mm:ss');
          socketBody.menuUrl = 'myTask';
          socketBody.sendUser = userid;
          socketBody.title = res.review_name + ',待您审批';
          if (userIds.length > 0) {
            _this.sendMsgByUserAction(socketBody, userIds, 'msg_box');
            apiBpmnService.pushDataToBacklogPeople(userIds, res.form_type, res.data, res.review_name, res.procInstId);
          }
          aj.result = result;
        } else {
          aj.success = false;
          aj.msg = '申请立案失败';
        }
      } else {
        aj.success = false;
        aj.msg = '申请立案失败';
      }

      // 立案成功推送到web端
      const userIds = yield _this.getOnlineModuleByUser('jdzh');

      // let obj = this.getSocketMsgBody(result);
      // let socketBody = {
      //     result: {
      //         id: data.id,
      //         formModuleName: 'jdsl'
      //     },
      //     moduleName: 'jdzh',
      //     menuUrl: 'controlCenter'
      // };
      const socketBody = _this.getSocketMsgBody();
      socketBody.result = {
        id: data.id,
        formModuleName: 'jdsl'
      };
      socketBody.title = '事件立案';
      socketBody.menuUrl = 'controlCenter';
      _this.sendMsgByUserAction(socketBody, userIds, 'control_center');

      return _this.successJson(aj);
    })();
  }
  caseByIdAction() {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const aj = think.ajaxJson();
      const data = _this2.post();
      const applyCaseService = _this2.service('oa/CaseService');
      const result = yield applyCaseService.caseById(data);
      if (!result.error) {
        aj.result = result;
      } else {
        aj.success = false;
        aj.msg = '查询失败';
      }
      return _this2.json(aj);
    })();
  }

  /**
     * 部件立案申请
     */
  applyPartCaseAction() {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      const aj = think.ajaxJson();
      const data = _this3.post();

      if (!data.case_id) {
        data.case_id = think.uuid('v1');
      }
      const key = data.process_id;
      const id = data.org_id;
      const userid = data.userid;
      const overtime = data.overtime;
      const BpmnService = _this3.service('bpmn/BpmnService');
      const applyCaseService = _this3.service('oa/CaseService');
      const apiBpmnService = _this3.service('api/bpmn/BpmnService');

      const res = yield BpmnService.startProcessInstance(key, id, userid, null, overtime);

      if (!res.error) {
        data.proc_inst_id = res.procInstId;
        const result = yield applyCaseService.createPartCase(data);
        if (!result.error) {
          // let socketBody = {
          //     result: {
          //         id: data.case_id,
          //         formModuleName: 'jdsl'
          //     },
          //     moduleName: 'jdzh',
          //     menuUrl: 'controlCenter'
          // };
          //
          // this.sendMsgByModuleAction(socketBody);
          const userIds = res.backLogsUserIds;
          // let socketBody = {
          //     result: {
          //         id: res.procInstId,
          //         date: res.date
          //     },
          //     moduleName: 'xtbg',
          //     userIds: userid,
          //     menuUrl: 'myTask'
          // };
          const socketBody = _this3.getSocketMsgBody();
          socketBody.result = {
            id: res.procInstId,
            date: res.date
          };
          socketBody.menuUrl = 'myTask';
          socketBody.sendUser = userid;
          socketBody.sendDate = (0, _moment2.default)().format('YYYY-MM-DD HH:mm:ss');
          socketBody.title = res.review_name + ',待您审批';
          if (userIds.length > 0) {
            _this3.sendMsgByUserAction(socketBody, userIds, 'msg_box');
            apiBpmnService.pushDataToBacklogPeople(userIds, res.form_type, res.data, res.review_name, res.procInstId);
          }
          aj.result = res;
        } else {
          aj.success = false;
          aj.msg = '申请立案失败';
        }
      } else {
        aj.success = false;
        aj.msg = '申请立案失败';
      }
      // 立案成功推送到web端
      const userIds = yield _this3.getOnlineModuleByUser('jdzh');
      // 就事件随机推送给某个用户
      // const randomId = userIds[Math.floor(Math.random() * userIds.length)];

      // console.log(randomId, 'randomId');
      // let obj = this.getSocketMsgBody(result);
      const socketBody = _this3.getSocketMsgBody();
      socketBody.result = {
        id: data.id,
        formModuleName: 'jdsl'
      };
      socketBody.title = '事件立案';
      socketBody.menuUrl = 'controlCenter';
      _this3.sendMsgByUserAction(socketBody, userIds, 'control_center');

      return _this3.successJson();
    })();
  }

  /**
     * 更新部件表BGID: updateBGID
     */

  updateCaseBGIDAction() {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      const aj = think.ajaxJson();
      const data = _this4.post();
      const updateCaseBGIDService = _this4.service('oa/CaseService');
      const res = yield updateCaseBGIDService.updateCaseBGID(data);

      if (!res.error) {
        aj.result = res;
      } else {
        aj.success = false;
        aj.msg = '更新失败';
      }

      return _this4.json(aj);
    })();
  }

  /**
     * 查询案件表列表
     */

  get_all_case_listAction() {
    var _this5 = this;

    return _asyncToGenerator(function* () {
      const data = _this5.post();
      try {
        const res = yield _this5.service('oa/CaseService').all_case_list(data);
        return _this5.successJson(res);
      } catch (error) {
        return _this5.errorJson('查询失败');
      }
    })();
  }

  /**
     * 条件查询案件列表
     */

  get_specific_case_listAction() {
    var _this6 = this;

    return _asyncToGenerator(function* () {
      const data = _this6.post();
      try {
        const res = yield _this6.service('oa/CaseService').specific_case_list(data);
        return _this6.successJson(res);
      } catch (error) {
        return _this6.errorJson('查询失败');
      }
    })();
  }

  /**
     * 删除案件
     */
  delete_caseAction() {
    var _this7 = this;

    return _asyncToGenerator(function* () {
      const data = _this7.post();
      try {
        const res = yield _this7.service('oa/CaseService').delete_case(data);
        return _this7.successJson(res);
      } catch (error) {
        return _this7.errorJson('删除失败');
      }
    })();
  }
};